import createAxiosClient from "@/app/utils/axiosRequest";
import { decodeFromBase64, encodeToBase64 } from "next/dist/build/webpack/loaders/utils";
import cookie from "react-cookies";
import { UserName } from "@/app/constants/authTokenConstants";

// 搜索参数接口
export interface SearchParams {
    /*搜索关键词 */
    keyword?: string;

    /*是否搜索内容 */
    searchContent?: boolean;

    /*是否搜索标题 */
    searchTitle?: boolean;

    /*是否搜索标签 */
    searchTags?: boolean;

    /*包含标签列表 */
    includeTags?: string[];

    /*集合ID */
    collectionId?: number;

    /*是否包含公开笔记 */
    includePublic?: boolean;

    /*页码，从0开始 */
    page?: number;

    /*每页大小 */
    size?: number;
}

/**
 * MySQL高级搜索服务
 * @param params 搜索参数
 * @returns 搜索结果
 */
export async function searchNotesWithMysql(params: SearchParams) {
    //axios统一配置中心
    const axiosInstance = createAxiosClient();

    return await axiosInstance.post("/app/search/mysql/notes", {
        ...params,
        keyword: encodeToBase64(params.keyword || ""), // 加密关键词
        username: decodeFromBase64(cookie.load(UserName))
    }).then(response => {
        return response.data;
    }).catch(error => {
        console.error("MySQL搜索出错:", error);
        return error.data;
    });
}

/**
 * ElasticSearch高级搜索服务
 * @param params 搜索参数
 * @returns 搜索结果
 */
export async function searchNotesWithES(params: SearchParams) {
    //axios统一配置中心
    const axiosInstance = createAxiosClient();

    return await axiosInstance.post("/app/search/notes", {
        ...params,
        keyword: encodeToBase64(params.keyword || ""), // 加密关键词
        username: decodeFromBase64(cookie.load(UserName))
    }).then(response => {
        return response.data;
    }).catch(error => {
        console.error("ES搜索出错:", error);
        return error.data;
    });
} 